/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FuViewPanel.java
 *
 * Created on 29/11/2011, 21:21:05
 */

package proj5labbd.graphics.funcionario;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import proj5labbd.DBInterface;
import proj5labbd.graphics.abstractOpGraphics.OperationPanel;

public class FuViewPanel extends OperationPanel {

    private ArrayList<String> fields;
    /** Creates new form FuViewPanel */
    public FuViewPanel(FuncionarioFrame frame, DBInterface dbInterface) {
        super(frame,dbInterface);
        initComponents();
        fields = new ArrayList<String>();
        FuncAction act = new FuncAction();
        jbtView.addActionListener(act);
        jchkCPF.addItemListener(act);
        jchkDate.addItemListener(act);
        jchkNome.addItemListener(act);
        jchkSalario.addItemListener(act);
        jchkTipo.addItemListener(act);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnField = new javax.swing.JPanel();
        jchkCPF = new javax.swing.JCheckBox();
        jchkDate = new javax.swing.JCheckBox();
        jchkNome = new javax.swing.JCheckBox();
        jchkSalario = new javax.swing.JCheckBox();
        jchkTipo = new javax.swing.JCheckBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbView = new javax.swing.JTable();
        jbtView = new javax.swing.JButton();
        jpnInsert = new javax.swing.JPanel();
        jlbCPF = new javax.swing.JLabel();
        jftxtCPF = new javax.swing.JFormattedTextField();
        jlbNome = new javax.swing.JLabel();
        jtxtNome = new javax.swing.JTextField();
        jlbDate = new javax.swing.JLabel();
        jlbSalario = new javax.swing.JLabel();
        jlbTipo = new javax.swing.JLabel();
        jtxtTipoFu = new javax.swing.JTextField();
        jftxtDate = new javax.swing.JFormattedTextField();
        jtxtSalario = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Visualização"));

        jpnField.setBorder(javax.swing.BorderFactory.createTitledBorder("Campos a selecionar"));

        jchkCPF.setText("CPF");

        jchkDate.setText("Data de nascimento");

        jchkNome.setText("Nome Completo");

        jchkSalario.setText("Salário");

        jchkTipo.setText("Tipo de funcionário");

        javax.swing.GroupLayout jpnFieldLayout = new javax.swing.GroupLayout(jpnField);
        jpnField.setLayout(jpnFieldLayout);
        jpnFieldLayout.setHorizontalGroup(
            jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jchkCPF)
                    .addComponent(jchkDate)
                    .addComponent(jchkNome)
                    .addComponent(jchkSalario)
                    .addComponent(jchkTipo))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        jpnFieldLayout.setVerticalGroup(
            jpnFieldLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnFieldLayout.createSequentialGroup()
                .addComponent(jchkCPF)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkDate)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkSalario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkTipo)
                .addContainerGap(60, Short.MAX_VALUE))
        );

        jtbView.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbView);

        jbtView.setText("Visualizar");

        jpnInsert.setBorder(javax.swing.BorderFactory.createTitledBorder("Parâmetros de busca"));

        jlbCPF.setText("CPF");

        try {
            jftxtCPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jftxtCPF.setFocusLostBehavior(javax.swing.JFormattedTextField.COMMIT);

        jlbNome.setText("Nome Completo");

        jlbDate.setText("Data de nascimento");

        jlbSalario.setText("Salário");

        jlbTipo.setText("Tipo de funcionário");

        try {
            jftxtDate.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("  -  -    ")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        javax.swing.GroupLayout jpnInsertLayout = new javax.swing.GroupLayout(jpnInsert);
        jpnInsert.setLayout(jpnInsertLayout);
        jpnInsertLayout.setHorizontalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jlbTipo)
                    .addGroup(jpnInsertLayout.createSequentialGroup()
                        .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jtxtTipoFu, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlbCPF, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jftxtCPF, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                            .addComponent(jtxtNome, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlbNome, javax.swing.GroupLayout.Alignment.LEADING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtxtSalario)
                            .addComponent(jftxtDate, 0, 0, Short.MAX_VALUE)
                            .addComponent(jlbDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlbSalario, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpnInsertLayout.setVerticalGroup(
            jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnInsertLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbCPF)
                    .addComponent(jlbDate))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jftxtCPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jftxtDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbNome)
                    .addComponent(jlbSalario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnInsertLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtxtSalario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jlbTipo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtxtTipoFu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 619, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jpnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jpnField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addComponent(jbtView, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpnInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jpnField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtView))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtView;
    private javax.swing.JCheckBox jchkCPF;
    private javax.swing.JCheckBox jchkDate;
    private javax.swing.JCheckBox jchkNome;
    private javax.swing.JCheckBox jchkSalario;
    private javax.swing.JCheckBox jchkTipo;
    private javax.swing.JFormattedTextField jftxtCPF;
    private javax.swing.JFormattedTextField jftxtDate;
    private javax.swing.JLabel jlbCPF;
    private javax.swing.JLabel jlbDate;
    private javax.swing.JLabel jlbNome;
    private javax.swing.JLabel jlbSalario;
    private javax.swing.JLabel jlbTipo;
    private javax.swing.JPanel jpnField;
    private javax.swing.JPanel jpnInsert;
    private javax.swing.JTable jtbView;
    private javax.swing.JTextField jtxtNome;
    private javax.swing.JTextField jtxtSalario;
    private javax.swing.JTextField jtxtTipoFu;
    // End of variables declaration//GEN-END:variables

    class FuncAction implements ActionListener, ItemListener {
        
        /*
         * Controla os eventos de visualização
         */
        public void actionPerformed(ActionEvent e) {
            
            String [] titles = {"ID do funcionário","Nome Completo","Data de nascimento","CPF","Salário","ID do cargo"};
            ArrayList<String> tables = new ArrayList<String>();
            tables.add("funcionario");
            boolean join = false;
            
            try {
                ArrayList<String> param = new ArrayList<String>();
                String s[][];
                
                if (jtxtNome.getText().trim().compareTo("") != 0) {
                    param.add("UPPER(funcionario.nomeCompletoDe)=UPPER('"+jtxtNome.getText()+"')");                
                }
                if (jftxtCPF.getText().compareTo("   .   .   -  ") != 0) {
                    param.add("funcionario.cpfFu="+jftxtCPF.getText());
                }
                if (jftxtDate.getText().compareTo("  -  -    ") != 0) {
                    param.add("TO_DATE(funcionario.dataNascimentoFu,'DD-MM-YYYY')="
                            + "TO_DATE('"+jftxtDate.getText()+"','DD-MM-YYYY')");
                }
                if (jtxtSalario.getText().trim().compareTo("") != 0) {
                    param.add("funcionario.salarioFu="+jtxtSalario.getText());
                }
                if (jtxtTipoFu.getText().trim().compareTo("") != 0) {
                    tables.add("tipoFuncionario");
                    param.add("funcionario.idTipoFu=tipoFuncionario.idTipoFu");
                    join = true;
                }

                if (param.size() > 0 ) {
                    
                        String[] paramArray = new String[param.size()];
                        
                        if (fields.size() > 0) {
                            
                            if (jchkTipo.isSelected() && !join) {
                                    param.add("funcionario.idTipoFu=tipoFuncionario.idTipoFu"); 
                                    tables.add("tipoFuncionario");  
                            }
                       
                            String[] fieldArray = new String[fields.size()];
                            String[] tableArray = new String[tables.size()];
                            param.toArray(paramArray);
                            tables.toArray(tableArray);
                            fields.toArray(fieldArray);
                            s = dbInterface.view(fieldArray, paramArray, tableArray);
                            jtbView.setModel(
                                    new DefaultTableModel(s,fieldArray)
                                    );
                        }
                        else {
                            String[] tableArray = new String[tables.size()];
                            tables.toArray(tableArray);
                            s = dbInterface.view(paramArray, null, tableArray);
                            jtbView.setModel(
                                    new DefaultTableModel(s,titles)
                                    );
                        }
                    
                     
                 }
                
                else {
                    
                    if (fields.size() > 0) {
                            
                        if (jchkTipo.isSelected() && !join) {
                                param.add("funcionario.idTipoFu=tipoFuncionario.idTipoFu"); 
                                tables.add("tipoFuncionario");  
                        }
                        
                        String[] fieldArray = new String[fields.size()];
                        String[] tableArray = new String[tables.size()];
                        tables.toArray(tableArray);
                        fields.toArray(fieldArray);
                        
                        s = dbInterface.view(fieldArray, null, tableArray);
                        jtbView.setModel(
                                new DefaultTableModel(s,fieldArray)
                                );
                    }
                    else {
                        String[] tableArray = new String[tables.size()];
                        tables.toArray(tableArray);
                        s = dbInterface.view(null, null, tableArray);
                        jtbView.setModel(
                                        new DefaultTableModel(s,titles)
                                        );
                    }
                }
            }
            catch (SQLException ex) {
                frame.printException(ex);
                JOptionPane.showMessageDialog(null, "Erro ao resgatar as tuplas desejadas.");
            }
            
        }
         
        /*
         * Controla os eventos referentes às check box
         */
         public void itemStateChanged(ItemEvent ie) {
            if ( ie.getStateChange() == ItemEvent.SELECTED ) {
                if (ie.getSource() == jchkCPF) {
                    fields.add("funcionario.cpfFu");
                }
                if (ie.getSource() == jchkDate) {
                    fields.add("funcionario.dataNascimentoFu");
                }
                if (ie.getSource() == jchkNome) {
                    fields.add("funcionario.nomeCompletoFu");
                }
                if (ie.getSource() == jchkSalario) {
                    fields.add("funcionario.salarioFu");
                }
                if (ie.getSource() == jchkTipo) {
                    fields.add("tipoFuncionario.descricaoTipoFu");
                }
            }
            if ( ie.getStateChange() == ItemEvent.DESELECTED ) {
                if (ie.getSource() == jchkCPF) {
                    fields.remove("funcionario.cpfFu");
                }
                if (ie.getSource() == jchkDate) {
                    fields.remove("funcionario.dataNascimentoFu");
                }
                if (ie.getSource() == jchkNome) {
                    fields.remove("funcionario.nomeCompletoFu");
                }
                if (ie.getSource() == jchkSalario) {
                    fields.remove("funcionario.salarioFu");
                }
                if (ie.getSource() == jchkTipo) {
                    fields.remove("tipoFuncionario.descricaoTipoFu");
                }
            }
        }
        
    }
    
}
